package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.stream.Collectors;

//套餐管理
@RequestMapping
@RestController("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }


    //售卖状态
    @PostMapping("/status/{status}")
    public R<String> updatestatus(@RequestParam List<Long> ids, @PathVariable Integer status) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }

        return R.success("操作成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页
        Page<Setmeal> page1 = new Page<>();
        Page<SetmealDto> page2 = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //like查询,先判断非空
        qw.like(name != null, Setmeal::getName, name);
        //排序条件,按时间排序
        qw.orderByDesc(Setmeal::getUpdateTime);
        //调方法
        setmealService.page(page1, qw);
        //对象拷贝
        BeanUtils.copyProperties(page1, page2, "records");
        List<Setmeal> records = page1.getRecords();

        //拉姆达
        List<SetmealDto> list = records.stream().map((it) -> {

            SetmealDto setmealDto = new SetmealDto();
            //拷贝基本数据
            BeanUtils.copyProperties(it, setmealDto);
            Long categoryId = it.getCategoryId();
            Category category = categoryService.getById(categoryId);

            String name1 = category.getName();
            setmealDto.setCategoryName(name1);


            return setmealDto;
        }).collect(Collectors.toList());

        page2.setRecords(list);
        return R.success(page2);


    }
    //删除套餐
    @DeleteMapping

    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids: {}",ids);

        return R.success("删除成功");
    }


    //根据条件查询套餐数据,套餐实体,判断分类和状态
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        qw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(qw);

        return R.success(list);
    }


























}
